package com.cjl.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjl.anno.OperLog;
import com.cjl.auth.service.SysRoleService;
import com.cjl.commons.exception.OAException;
import com.cjl.commons.result.Result;
import com.cjl.commons.result.ResultCodeEnum;
import com.cjl.model.system.SysRole;
import com.cjl.vo.system.AssignRoleVo;
import com.cjl.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "获取角色")
    @GetMapping("/toAssign/{userId}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @OperLog(businessType = "SELECT", title = "角色管理")
    public Result toAssign(@PathVariable("userId") Long userId){
        Map<String, Object> map = sysRoleService.findRoleDataByUserId(userId);
        System.out.println(map);
        return Result.ok(map);
    }

    @ApiOperation(value = "为用户分配角色")
    @PostMapping("/doAssign")
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @OperLog(businessType = "UPDATE", title = "角色管理")
    public Result doAssign(@RequestBody AssignRoleVo assignRoleVo){
        sysRoleService.doAssign(assignRoleVo);
        return Result.ok();
    }


    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("findAll")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @OperLog(businessType = "SELECT", title = "角色管理")
    public Result<List<SysRole>> findAll(){
        List<SysRole> roleList = sysRoleService.list();
        return Result.ok(roleList);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    @OperLog(businessType = "SELECT", title = "角色管理")
    public Result pageQueryRole(@PathVariable("page") Long page,
                                @PathVariable("limit") Long limit,
                                SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if(!StringUtils.isEmpty(roleName)){
            wrapper.like(SysRole::getRoleName, roleName);
        }
        IPage<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    @OperLog(businessType = "SELECT", title = "角色管理")
    public Result get(@PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);
        return Result.ok(role);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "添加角色")
    @PostMapping("save")
    @OperLog(businessType = "INSERT", title = "角色管理")
    public Result save(@RequestBody @Validated SysRole role){
        boolean isSuccess = sysRoleService.save(role);
        return isSuccess ? Result.ok() : Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    @OperLog(businessType = "UPDATE", title = "角色管理")
    public Result updateById(@RequestBody SysRole role) {
        boolean isSuccess = sysRoleService.updateById(role);
        return isSuccess ? Result.ok() : Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("remove/{id}")
    @OperLog(businessType = "DELETE", title = "角色管理")
    public Result remove(@PathVariable Long id) {
        boolean isSuccess = sysRoleService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("batchRemove")
    @OperLog(businessType = "DELETE", title = "角色管理")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean isSuccess = sysRoleService.removeByIds(idList);
        return isSuccess ? Result.ok() : Result.fail();
    }
}

